//
//  OrderSelectPageView.m
//  RentTable
//
//  Created by mac on 2024/11/30.
//

#import "OrderSelectPageView.h"
#import "OsderSelectSubView.h"
#import "OrderSelectCountView.h"

@interface OrderSelectPageView()

@property(nonatomic, strong) UIView *orderBackView;

@property(nonatomic, strong) UIImageView *orderImageView;
@property (nonatomic, strong) UILabel *hotPriceLabel;
@property (nonatomic, strong) UILabel *hotPriceDayLabel;
@property (nonatomic, strong) UILabel *hotLabel;
@property (nonatomic, strong) UILabel *priceLabel;

@property (nonatomic, strong) OsderSelectSubView *optionSelectionView;
@property (nonatomic, strong) UIButton *tenantButton;

@property (nonatomic, strong) NSString *selectedCondition;
@property (nonatomic, strong) NSString *selectedColor;
@property (nonatomic, strong) NSString *selectedRentalPeriod;

@property (nonatomic, assign) NSInteger selectCount;
@end

@implementation OrderSelectPageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        self.selectedCondition = @"全新";
        self.selectedColor = @"黑色";
        self.selectedRentalPeriod = @"1个月";
        self.selectCount = 1;
        [self setUI];
    }
    return self;
}

- (void)setRentModel:(RentModel *)rentModel {
    _rentModel = rentModel;
    self.orderImageView.image = [UIImage imageNamed:rentModel.rentImage];
    self.hotPriceLabel.text = [NSString stringWithFormat:@"¥%.1f",rentModel.rentPrice];
    self.hotLabel.text = rentModel.rentTitle;
    self.priceLabel.text = [NSString stringWithFormat:@"押金：¥%@",rentModel.rentDeposit];;
}

- (void)setUI {
    
    self.backgroundColor = myRGBHexAlpha(0x000000, 0.6);
    [self setUserInteractionEnabled:true];
    
    self.orderBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 204 * scaleH, spDeviceWidth, spDeviceHeight - 204 * scaleH)];
    self.orderBackView.backgroundColor = myRGBHex(0xFFFFFF);
    [self.orderBackView setUserInteractionEnabled:true];
    [self addSubview:self.orderBackView];
    [self.orderBackView setCornerRadius:20 forCorners:UIRectCornerTopLeft | UIRectCornerTopRight borderColor:myRGBHex(0xFFFFFF) borderWidth:1];
    
    
    self.orderImageView = [UIImageView new];
    self.orderImageView.image = [UIImage imageNamed:@"1"];
    [self.orderBackView addSubview:self.orderImageView];
    self.orderImageView.layer.cornerRadius = 5;
    self.orderImageView.layer.masksToBounds = true;
    [self.orderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(19);
        make.top.mas_equalTo(25);
        make.size.mas_equalTo(CGSizeMake(83, 83));
    }];
    
    self.hotPriceLabel = [[UILabel alloc] init];
    self.hotPriceLabel.font = [UIFont pingFangSCSemiboldWithSize:16];
    self.hotPriceLabel.textColor = myRGBHex(0x333333);
    self.hotPriceLabel.text = @"¥ 59.9";
    self.hotPriceLabel.textAlignment = NSTextAlignmentLeft;
    [self.orderBackView addSubview:self.hotPriceLabel];
    [self.hotPriceLabel sizeToFit];
    [self.hotPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(22);
        make.leading.equalTo(self.orderImageView.mas_trailing).offset(19);
        make.height.mas_equalTo(25);
    }];
    
    self.hotPriceDayLabel = [[UILabel alloc] init];
    self.hotPriceDayLabel.font = [UIFont pingFangSCSemiboldWithSize:12];
    self.hotPriceDayLabel.textColor = myRGBHex(0x999999);
    self.hotPriceDayLabel.textAlignment = NSTextAlignmentLeft;
    self.hotPriceDayLabel.text = @"/天";
    [self.orderBackView addSubview:self.hotPriceDayLabel];
    [self.hotPriceDayLabel sizeToFit];
    [self.hotPriceDayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.hotPriceLabel.mas_bottom);
        make.leading.equalTo(self.hotPriceLabel.mas_trailing).offset(1);
        make.height.mas_equalTo(18);
    }];
    
    self.hotLabel = [[UILabel alloc] init];
    self.hotLabel.font = [UIFont pingFangSCRegularWithSize:12];
    self.hotLabel.textColor = myRGBHex(0x333333);
    self.hotLabel.text = @"台球桌标准型钢库中式黑八台球厅会所家用成人台球桌自动回球";
    self.hotLabel.textAlignment = NSTextAlignmentLeft;
    self.hotLabel.numberOfLines = 0;
    [self.orderBackView addSubview:self.hotLabel];
    [self.hotLabel sizeToFit];
    [self.hotLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hotPriceLabel.mas_bottom).offset(7);
        make.leading.equalTo(self.orderImageView.mas_trailing).offset(19);
        make.trailing.mas_equalTo(-13);
        make.height.lessThanOrEqualTo(@50);
    }];
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont pingFangSCSemiboldWithSize:14];
    self.priceLabel.textColor = myRGBHex(0x333333);
    self.priceLabel.textAlignment = NSTextAlignmentLeft;
    self.priceLabel.text = @"押金：¥2000";
    [self.orderBackView addSubview:self.priceLabel];
    [self.priceLabel sizeToFit];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hotLabel.mas_bottom).offset(8);
        make.leading.equalTo(self.orderImageView.mas_trailing).offset(19);
        make.height.mas_equalTo(18);
    }];
    
    self.optionSelectionView = [[OsderSelectSubView alloc] initWithFrame:CGRectMake(0, 136 * scaleH, spDeviceWidth, spDeviceHeight - 420 * scaleH)
                                                                   conditionOptions:@[@"全新", @"95新"]
                                                                      colorOptions:@[@"黑色", @"红色"]
                                                               rentalPeriodOptions:@[@"1个月", @"3个月", @"6个月"]
                                                                   optionSelected:^(NSString *selectedCondition, NSString *selectedColor, NSString *selectedRentalPeriod) {
        self.selectedCondition = selectedCondition;
        self.selectedColor = selectedColor;
        self.selectedRentalPeriod = selectedRentalPeriod;
    }];
    [self.optionSelectionView setUserInteractionEnabled:true];
    
    
    [self.orderBackView addSubview:self.optionSelectionView];
    
    OrderSelectCountView *countView = [[OrderSelectCountView alloc] initWithFrame:CGRectZero initialCount:1];
    QLWEAKSELF
    countView.countChanged = ^(NSInteger count) {
        weakSelf.selectCount = count;
    };
    [self.orderBackView addSubview:countView];
    [countView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(-119 * scaleH);
        make.height.mas_equalTo(30);
    }];
    
    self.tenantButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.tenantButton setTitle:@"立即租赁" forState:UIControlStateNormal];
    [self.tenantButton setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    self.tenantButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.tenantButton addTarget:self action:@selector(tenantButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.orderBackView addSubview:self.tenantButton];
    [self.tenantButton.titleLabel sizeToFit];
    self.tenantButton.layer.cornerRadius = 22;
    self.tenantButton.layer.masksToBounds = true;
    self.tenantButton.backgroundColor = myRGBHex(0x121212);
    [self.tenantButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-44 * scaleH);
        make.trailing.mas_equalTo(-16);
        make.leading.mas_equalTo(16);
        make.height.mas_equalTo(44 * scaleH);
    }];
}

- (void)tenantButtonPressed:(UIButton *)sender {
    [self removeFromSuperview];
    
    if (self.orderSelect) {
        self.orderSelect();
    }
    self.rentModel.rentCondition = self.selectedCondition;
    self.rentModel.rentColor = self.selectedColor;
    if ([self.selectedRentalPeriod isEqualToString:@"一个月"]) {
        self.rentModel.rentPeriod = 1;
    } else if ([self.selectedRentalPeriod isEqualToString:@"3个月"]) {
        self.rentModel.rentPeriod = 3;
    } else if ([self.selectedRentalPeriod isEqualToString:@"6个月"]) {
        self.rentModel.rentPeriod = 6;
    } else {

        self.rentModel.rentPeriod = 1;
    }
    
    self.rentModel.rentCount = self.selectCount;
    
    [[RentManager sharedManager] updateRentModel:self.rentModel];
    [[RentManager sharedManager] saveRentDataToFile];
}

@end
